package com.info.zhiduoduo.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.value.ApproveValueFormBean;
import com.info.zhiduoduo.admin.model.formbean.value.QueryValueFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.WxMessageService;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.value.ValueServeDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.memorandum.PlatformMemorandum;
import com.info.zhiduoduo.repository.entity.value.ValueServe;
import com.info.zhiduoduo.repository.mapper.value.ValueServeMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ValueService extends BaseService {

    @Resource
    private ValueServeMapper valueServeMapper;

    @Resource
    private WxMessageService wxMessageService;


    /**
     * 查询  增值服务列表
     * @param baseSimpleFormBean
     * @return
     */
    public SmartResult queryAllServeList(BaseSimpleFormBean<QueryValueFormBean> baseSimpleFormBean) {

        QueryValueFormBean formBean = baseSimpleFormBean.getReqdata();
        int id = baseSimpleFormBean.getUserid();
        if( id <= 0 ) return errorParamSmartResult();
        Integer state = formBean.getState();

        // 查询全部
        if( state == 4 ){
            state = null;
        }

        Integer type = formBean.getType();

        Page<ValueServeDto> page = PageHelper.startPage(baseSimpleFormBean.getPage(), baseSimpleFormBean.getRows());
        valueServeMapper.queryValueServeList(null, state, type);
        return smartResultPage(page);

    }

    /**
     * 审核操作
     * @param baseSimpleFormBean
     * @return
     */
    public SmartResult approveValueServe(BaseSimpleFormBean<ApproveValueFormBean> baseSimpleFormBean) {

        ApproveValueFormBean formBean = baseSimpleFormBean.getReqdata();
        ValueServe valueServe = new ValueServe().selectById(formBean.getId());
        if( valueServe == null  ) return smartResult(StatusMsg.StatusMsg_102);

        //审批操作  需要同步
        if( formBean.getState() == 1 && formBean.getIsSync() == 1 ){
            PlatformMemorandum memorandum = new PlatformMemorandum();
            memorandum.setCustomerType( valueServe.getCustomerType() )
                    .setCustomerId( valueServe.getCustomerId() )
                    .setCustomerName( valueServe.getName() )
                    .setTitle( formBean.getTitle() )
                    .setContent( formBean.getContent() )
                    .setType( formBean.getType() )
                    .setCreatetime( new Date() )
                    .setUpdatetime( new Date() )
                    .setFounderId( baseSimpleFormBean.getUserid() )
                    .setState(1);
            if( !memorandum.insert() ) return optFailSmartResult();
        }
        // 设置 驳回理由
        if( formBean.getState() == 3 ){
            if( StringUtils.isEmpty(formBean.getRejectionReason()) ) return errorParamSmartResult();
            valueServe.setRejectionReason( formBean.getRejectionReason() );
        }
        valueServe.setBackEmail(formBean.getBackEmail());
        valueServe.setTrackingNum(formBean.getTrackingNum());
        valueServe.setAddress(formBean.getAddress());
        valueServe.setSender(formBean.getSender());
        valueServe.setSealedBy(formBean.getSealedBy());
        valueServe.setNotes(formBean.getNotes());
        // 改变状态
        valueServe.setState( formBean.getState() );
        valueServe.setUpdatetime(new Date());

        if( !valueServe.updateById() ){
            return optFailSmartResult();
        }

        if (formBean.getState() == 1 && valueServe.getType() == 26) {
            Company company = new Company().selectById(valueServe.getCustomerId());
            if (company != null) {
                company.setViewMakerQuota(1).updateById();
            }
        }

        /*
        发微信通知给客户
         */
        //操作状态
        int flag = formBean.getState();
        //用户类型
        int userType = valueServe.getCustomerType();
        //服务类型
        int type = valueServe.getType();
        //客户id
        int id = valueServe.getCustomerId();
        //备注
        String remark = formBean.getRejectionReason();
        switch (flag) {
            case 1:{
                switch (userType) {
                    case 1:
                    case 5:
                    case 6:{
                        wxMessageService.makerAppreciationServiceAcceptedNotice(type, id, "审批", remark);
                    }
                    break;
                    case 2:{
                        wxMessageService.companyAppreciationServiceAcceptedNotice(type, id, "审批", remark);
                    }
                    break;
                    default: return optFailSmartResult();
                }
            }
            break;
            case 2:{
                switch (userType) {
                    case 1:
                    case 5:
                    case 6:{
                        wxMessageService.makerAppreciationServiceAcceptedNotice(type, id, "完成", remark);
                    }
                    break;
                    case 2:{
                        wxMessageService.companyAppreciationServiceAcceptedNotice(type, id, "完成", remark);
                    }
                    break;
                    default: return optFailSmartResult();
                }
            }
            break;
            case 3:{
                switch (userType) {
                    case 1:
                    case 5:
                    case 6:{
                        wxMessageService.makerAppreciationServiceAcceptedNotice(type, id, "驳回", "驳回原因：" + remark);
                    }
                    break;
                    case 2:{
                        wxMessageService.companyAppreciationServiceAcceptedNotice(type, id, "驳回", "驳回原因：" + remark);
                    }
                    break;
                    default: return optFailSmartResult();
                }
            }
            break;
            default: return optFailSmartResult();
        }

        return smartResult();
    }
}
